<?php
namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Rules\Admin\LoginRule;
use App\Services\Admin\LoginService;

class LoginController extends Controller
{
    /**
     * 登陆首页
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
     */
    public function index(Request $request){
//        $loginService = new LoginService();
//        $testLogin = session()->get('adminUserRule');
//        echo '<pre>';
//        print_r($testLogin);
//        echo '<pre>';
//        exit;
        if(!empty($request->session()->get('admin_user'))){
            return redirect('admin/index');
        }



        return view('admin.login.index');
    }

    /**
     * 登陆处理
     * @param LoginRule $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function login(LoginRule $request){
        //登陆操作
        $loginService = new LoginService();

        return $loginService->login($request);
    }

    /**
     * 登出
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function loginOut(Request $request){
        $request->session()->forget('admin_user');
        return redirect('admin/loginIndex');
    }

    /**
     * 页面出错提示
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function errorIndex(Request $request){
        $errorMsg = session()->get('errorMsg');
        $errorReturnName = session()->get('errorReturnName');
        $errorReturnUrl = session()->get('errorReturnUrl');
        session()->forget(['errorMsg', 'errorReturnName', 'errorReturnUrl']);
        return view('admin/login/errorIndex', [
            'errorMsg' => $errorMsg,
            'errorReturnName' => $errorReturnName,
            'errorReturnUrl' => $errorReturnUrl,
        ]);
    }

}
